package com.pk.android.view.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.pk.android.R;
import com.pk.android.model.League;
import com.pk.android.model.Team;

public class ExpandListAdapter extends BaseExpandableListAdapter
{

	private Context context;
	private List<League> groups;
	//private boolean isCollapsed;
	//private int groupIndex;
	
	public ExpandListAdapter(Context context, List<League> groups) {
		this.context = context;
		this.groups = groups;
	}
	
	public void addItem(Team item, League group) {
		if (!groups.contains(group)) {
			groups.add(group);
		}
		int index = groups.indexOf(group);
		List<Team> ch = groups.get(index).getTeams();
		ch.add(item);
		groups.get(index).setTeams(ch);
	}
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		List<Team> chList = groups.get(groupPosition).getTeams();
		return chList.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
			ViewGroup parent) {
		Team child = (Team) getChild(groupPosition, childPosition);
		if (view == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = infalInflater.inflate(R.layout.expandlist_team, null);
		}
		TextView tv = (TextView) view.findViewById(R.id.temp_child_text);
		tv.setText(child.getName().toString());
		//tv.setTag(child.getPoints());
		// TODO Auto-generated method stub
		
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		List<Team> chList = groups.get(groupPosition).getTeams();

		return chList.size();

	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupCollapsed(groupPosition);
		//groupIndex = groupPosition;
		//isCollapsed = true;
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupExpanded(groupPosition);
		//groupIndex = groupPosition;
		//isCollapsed = false;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isLastChild, View view,
			ViewGroup parent) {
		League group = (League) getGroup(groupPosition);
		if (view == null) {
			LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inf.inflate(R.layout.expandlist_league, null);
		}
		TextView tv = (TextView) view.findViewById(R.id.temp_group_text);
		tv.setText(group.getName());
		
		if(((ExpandableListView)parent).isGroupExpanded(groupPosition))
			((ImageView)view.findViewById(R.id.img_list_collapse)).setImageResource(R.drawable.ic_action_expand);
		else
			((ImageView)view.findViewById(R.id.img_list_collapse)).setImageResource(R.drawable.ic_action_collapse);
		
		// TODO Auto-generated method stub
		return view;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

}


